package com.lzx.applib.base;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.view.WindowManager;

import com.lzx.applib.R;
import com.lzx.applib.okhttp.download.DownloadInfoTable;
import com.lzx.applib.utils.DpSpDip2Px;
import com.lzx.applib.utils.PreferenceUtil;
import com.lzx.applib.utils.ToastUtil;

import java.io.File;

/**
 * Created by Lzx on 16/10/20 0020.
 */

public class BaseApplication extends Application {

    public static Context context;
    public static String packageName;


    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        packageName = getPackageName();

        ToastUtil.init(this);
        DpSpDip2Px.init(this);
        PreferenceUtil.init(this);
        initFolder();
        initScreenSize();

        DownloadInfoTable.init(this);
    }


    public static File BaseFolder;
    public static File PicStoreFolder;
    public static File PicThumbFolder;
    public static File PicCacheFolder;
    public static File PicUploadFolder;

    public static File DataFolder;
    public static File DataCacheFolder;
    public static File DataStoreFolder;
    public static File DownloadFolder;

    public static int mScreenWidth, mScreenHeight;

    public void initFolder() {
        String BaseFolderPath = null;
        if (TextUtils.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
            BaseFolderPath = Environment.getExternalStorageDirectory() + File.separator +
                    getString(R.string.app_name);
        } else {
            BaseFolderPath = getFilesDir().getAbsolutePath();
        }
        BaseFolder = new File(BaseFolderPath);
        checkFolder(BaseFolder);

        PicStoreFolder = new File(BaseFolder, "Picture");
        checkFolder(PicStoreFolder);
        PicThumbFolder = new File(PicStoreFolder, "Thumb");
        checkFolder(PicThumbFolder);
        PicCacheFolder = new File(PicStoreFolder, "Cache");
        checkFolder(PicCacheFolder);
        PicUploadFolder = new File(PicStoreFolder, "Upload");
        checkFolder(PicUploadFolder);

        DataFolder = new File(BaseFolder, "Data");
        checkFolder(DataFolder);
        DataCacheFolder = new File(DataFolder, "Cache");
        checkFolder(DataCacheFolder);
        DataStoreFolder = new File(DataFolder, "Store");
        checkFolder(DataStoreFolder);

        DownloadFolder = new File(BaseFolder, "Download");
        checkFolder(DownloadFolder);

    }

    public void checkFolder(File file) {
        if (!isDir(file)) {
            file.delete();
            file.mkdirs();
        }
    }

    public boolean isDir(File file) {
        if (!file.exists()) return false;
        return file.isDirectory();
    }

    public void initScreenSize() {
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mScreenWidth = Math.min(wm.getDefaultDisplay().getWidth(), wm.getDefaultDisplay().getHeight());
        mScreenHeight = Math.max(wm.getDefaultDisplay().getWidth(), wm.getDefaultDisplay().getHeight());
    }

    public static File getThumbImageFile(String path) {
        return new File(PicThumbFolder, path.hashCode()+".thumb");
    }

    public static File getInfoFile(String path) {
        return new File(PicThumbFolder, path.hashCode()+".info");
    }

    public static File getDownloadFile(String path) {
        return new File(DownloadFolder, path.hashCode() + ".video");
    }

}
